import 'package:flutter/material.dart';
import 'package:flutter_demo/ui/pages/read/tools/book_controller.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class ReadState {
  //翻页控制器
  late BookController bookController;
  //字号 文字大小
  double fontSize = 18.0;
  // 书籍内容
  String data = "";

  //可视区域文本最大高度
  double maxTextHeight = 0;

  // 书籍高度
  late double maxHeight = 500;

  // 段落间距
  final double fontHeight = 1.4;

  //二分查找最大的文字数量
  int binarySearchMaxChar = 1000;

  //存储每页存储的最后一个字的位置，下标是页号
  List allPages = [];

  //是否加载完成
  bool isOver = false;

  //书籍边距
  final EdgeInsetsGeometry padding = const EdgeInsetsDirectional.all(15);

  // 书籍宽度
  late double maxWith = 1.sw;

  /// 每页最多1000字计算， 二分查找
  Future loadPages() async {
    allPages.clear();
    print("开始${DateTime.now()}");
    return await Future(() {
      //文字在文档中的位置
      int index = 0;
      allPages.add(index);
      //字数左指针
      int left = index;
      //字数右指针
      int right = 1000;
      while (true) {
        if (right >= data.length) {
          //尾页不需要处理最后一行
          right = data.length;
          print("尾页");
          if (calculateTextHeight(data.substring(index, right), fontSize) <=
              maxTextHeight) {
            allPages.add(right);
            print("结束");
            print("allPages ${allPages.toString()}");
            print("结束${DateTime.now()}");
            print("结束循环次数$num");
            return;
          }
        }
        int mid = (left + right) ~/ 2;
        //是否满足屏幕高度
        if ((right < left) ||
            calculateTextHeight(data.substring(index, mid), fontSize) ==
                maxTextHeight) {
          for (int i = 0; i < 100; i++) {
            //处理最后一行是否满足行内字数
            if (calculateTextHeight(data.substring(index, mid + i), fontSize) >
                maxTextHeight) {
              index = mid + i - 1;
              left = index;
              right = index + 1000;
              //处理最后一行之后的换行符
              if (index < data.length - 1 &&
                  data.substring(index, index + 1).startsWith('\n')) {
                index += 1;
              }
              allPages.add(index);
              break;
            }
          }
        } else if (calculateTextHeight(data.substring(index, mid), fontSize) <
            maxTextHeight) {
          left = mid + 1;
        } else if (calculateTextHeight(data.substring(index, mid), fontSize) >
            maxTextHeight) {
          right = mid - 1;
        }
      }
    });
  }

  /// 获取文字高度
  double calculateTextHeight(
    String value,
    double fontSize,
  ) {
    TextPainter painter = TextPainter(
        locale: WidgetsBinding.instance.window.locale,
        textDirection: TextDirection.ltr,
        maxLines: 1000,
        text: TextSpan(
            text: value,
            style: TextStyle(
              height: fontHeight,
              // fontWeight: fontWeight,
              fontSize: fontSize,
            )));
    painter.layout(maxWidth: maxWith - padding.horizontal);
    return painter.size.height;
  }
}
